﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/25/2009
 * Time: 10:19 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using NHibernate;
using System.Collections.Generic;
using System.Data;
using Miming.Model.Dao.Interface;

namespace Miming.Model.Dao.NHibernate
{
	/// <summary>
	/// Description of NHibernateItemDao.
	/// </summary>
	public class NHibernateItemDao : IItemDao
	{
		public NHibernateItemDao()
		{
		}
		
		public void SaveOrUpdate(Item item)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.SaveOrUpdate(item);
			session.Flush();
		}
		
		public void Delete(Item item)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.Delete(item);
			session.Flush();
		}
		
		public Item ReadByPk(int id)
		{
			return NHibernateHelper.OpenSession().Load<Item>(id);
		}
		
		public IList<Item> FindByCode(string code)
		{
			return NHibernateHelper.OpenSession().CreateQuery("from Item where PartNumber like '%" + code + "%'").List<Item>();
		}
		
		public IList<Item> FindAll()
		{
			return NHibernateHelper.OpenSession().CreateCriteria(typeof(Item)).List<Item>();
		}
	}
}
